#ifndef __ARRAY_2_TREE_H__
#define __ARRAY_2_TREE_H__
#include "TreeNode.cpp"

/** 将树结构存放在数组当中
 *　 数组的前两个字节存放数组最大长度
 *  第３，４字节存放当前使用的长度
 *  ５，６字节存放树的根索引
 *
 */

template<typename T>
class Array2Tree
{
public:
	typedef void(*filterFunction)(const TreeNode<T> t);
	Array2Tree(int arrayLength);
	bool insert(const TreeNode<T> &node);
	bool insert(const T& t);
	const unsigned short getLength();
	const unsigned short getinUse();
	const unsigned short getRootIndex();
    const TreeNode<T>* getArray();
	void setFilterFun(filterFunction pfun);
	void oneByone();
	~Array2Tree();
private:
	char* parray;
	TreeNode<T> *pTreeArray;

	static const char LENGTH_INDEX = 0;
	static const char INUSE_INDEX = 1;
	static const char ROOT_INDEX = 2;
	static const int  EXT_LENGTH = sizeof(short) * 3;
	unsigned short getIndexValue(unsigned short index);
	void setIndexValue(unsigned short index, unsigned short value);
	void initIndex(int totalLength);
	bool innerInsert(unsigned short rootIndex, unsigned short nodeIndex);
	filterFunction filter;
	void innerOneByone(int rootIndex);
};

#endif // __ARRAY2TREE_H__
